/**
 * Created by vincent on 2017/9/29.
 */
moduleDynamicHtml.controller('EditHtml', function ($$CambioConfig, $scope, $log, LoadingService, DialogService, DynamicHtmlResource, UploadFileService) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
    });

    $scope.upload = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'FILE', $scope).then(function (o) {
                $scope.editingHtml.zip = o.id;
                $scope.editingHtml.folderPath = "NA";
            });
        }
    };

    $scope.download = function (editingHtml) {
        if (!StringUtils.isNlOrUndOrEmpty(editingHtml.zip)) {
            window.open($$CambioConfig.DOWNLOAD_URL.format({id: editingHtml.zip}), '_blank');
        }
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        if ($scope.editDHForm.$valid) {
            //LoadingService.show();
            Alert.confirm(function () {
                DynamicHtmlResource.update($scope.editingHtml, function (htmlResult) {
                    $log.debug("h5项目更新结果", htmlResult);
                    angular.copy(htmlResult, $scope.currentHtml);
                    DialogService.close();
                    Alert.success("更新项目信息成功！");
                    //LoadingService.hide();
                });
            })
        }
    };

});